
//
//  ByCellMemberStorePhoneOrPrice.m
//  mijifen_ios
//
//  Created by mc on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellMemberStorePhoneOrPrice.h"

@interface ByCellMemberStorePhoneOrPrice()<UITextFieldDelegate>
@end

@implementation ByCellMemberStorePhoneOrPrice
{
    UILabel     *_titleLabel;
    UIView      *_rightView;
    UITextField *_textField;
    
    NSString    *_title;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView andTitle:(NSString *)title{
    
    static NSString *cellIdentifier = @"ByCellMemberStorePhoneOrPrice";
    ByCellMemberStorePhoneOrPrice *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andTitle:title];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitle:(NSString *)title{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _title = title;
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard) name:@"isShowMemberStore" object:nil];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _titleLabel = [[UILabel alloc] init];
    [self addSubview:_titleLabel];
    CGFloat width = [@"储值优惠信息" getSizeOfString:16].width + 30;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(width, 50));
    }];
    _titleLabel.text = _title;
    _titleLabel.font= FONT(16);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.layer.cornerRadius = 4.0;
    _titleLabel.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    _titleLabel.layer.borderWidth = 1;
    
    _rightView = [[UIView alloc] init];
    [self addSubview:_rightView];
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(10);
        make.top.equalTo(_titleLabel.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
    _textField = [[UITextField alloc] init];
    [_rightView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_rightView.mas_top).offset(10);
        make.left.equalTo(_rightView.mas_left).offset(5);
        make.bottom.equalTo(_rightView.mas_bottom).offset(-10);
        make.right.equalTo(_rightView.mas_right).offset(-5);
    }];
    _textField.tag = 100;
    _textField.delegate = self;
    _rightView.backgroundColor = [UIColor whiteColor];
    _rightView.layer.cornerRadius = 4.0;
    _rightView.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    _rightView.layer.borderWidth = 1;
    
    
    if ([_title isEqualToString:@"储值优惠信息"]) {
        _textField.placeholder = @"例如：会员储值100，返20";
    }
    _textField.font = FONT(16);
    _textField.adjustsFontSizeToFitWidth = YES;

}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.editBlock) {
        self.editBlock(textField.text);
    }
}

#pragma mark - 隐藏键盘
- (void)hideKeyboard{
    [_textField resignFirstResponder];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"isShowMemberStore" object:nil];
}

@end
